package com.gpay.erp.web.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.gpay.erp.core.dao.page.PageHelper;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.BankCard;
import com.gpay.erp.services.system.BankcardInteface;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;
import com.gpay.erp.web.vo.UserSessionInfor;

/**
 * 银行卡信息管理
 * 
 * @author lg
 * 
 */
@Controller
@RequestMapping("/bankcardController/")
public class BankcardController extends BaseController {
	private Log log = LogFactory.getLog(BankcardController.class);
	@Autowired
	private BankcardInteface bankcardService;

	// 页面跳转 银行卡管理
	@RequestMapping("selectBank")
	public String bankCard() {
		return "system/bank/selectBank";
	}

	// 页面跳转 银行卡新增
	@RequestMapping("addBank")
	public String addbank() {
		return "system/bank/addBank";
	}

	// 页面跳转 银行卡修改
	@RequestMapping("updateBank")
	public String updatebank() {
		return "system/bank/updateBank";
	}

	/*
	 * 点击银行卡管理，进行分页查询
	 * 
	 * @param pageHelper：分页实体类
	 * 
	 * @param BankCard{ cardNo:账号, cardName:账户名称}
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("selectBankcard")
	public void selectBankcard(UserSessionInfor userSessionInfor,
			PagerModel pagerModel, BankCard bankCard, PageHelper pageHelper,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* 当前页 */
		int pagenow = pageHelper.getPage();
		/* 每页显示记录数 */
		int rows = pageHelper.getRows();
		/* 分页信息填充进入PagerModel中 */
		bankCard.setPageSize(rows);
		bankCard.setFromRow((pagenow - 1) * rows);
		bankCard.setToRow(pagenow * rows);
		bankCard.setSort(pageHelper.getSort());
		bankCard.setOrder(pageHelper.getOrder());
		bankCard.setStatus(userSessionInfor.getStatus());
		/* 查询数据，返回PagerModel */
		pagerModel = bankcardService.selectPageList(bankCard);
		log.info("selectBankcard end");
		if (pagerModel.getRows().size() > 0) {
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击新增，增加银行卡信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("addBankcard")
	public void addBankcard(BankCard bankCard, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* 执行新增操作，返回t_bankCard表新增行的id */
		int flag = bankcardService.insert(bankCard);
		if (flag != 0) {
			log.info("t_bankCard表插入行的id值为：" + flag);
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("add.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("add.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击明细，根据ID查看银行卡详细信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("detailBankcard")
	public void detailBankcard(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		PagerModel pagerModel = new PagerModel();
		BankCard bankCard = new BankCard();
		/* 获取前端传入的id,判断是否为null */
		if (bankCard.getId() != null) {
			/* 根据ID查询明细 */
			pagerModel = bankcardService.selectById(bankCard.getId());
			if (pagerModel != null) {
				responseInfo.setResult("00010");
				responseInfo.setMessage(requestContext
						.getMessage("select.success"));
			} else {
				responseInfo.setResult("00011");
				responseInfo.setMessage(requestContext
						.getMessage("select.fail"));
			}
		} else {
			responseInfo.setResult("00011");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，修改银行卡信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("updateBankcard")
	public void updateBankcard(BankCard bankCard, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		if (bankCard.getId() != null) {
			/* 执行银行卡信息修改操作，返回t_bankCard表修改行的id */
			int flag = bankcardService.update(bankCard);
			if (flag != 0) {
				log.info("修改记录的id值为：" + flag);
				responseInfo.setResult("00030");
				responseInfo.setMessage(requestContext
						.getMessage("update.success"));
			} else {
				responseInfo.setResult("00031");
				responseInfo.setMessage(requestContext
						.getMessage("update.fail"));
			}
		} else {
			responseInfo.setResult("00031");
			responseInfo.setMessage(requestContext.getMessage("update.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击删除，根据id删除银行卡信息
	 * 
	 * @param ids[]
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("deleteBankcard")
	public void deleteBankcard(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* 接收前端传输的id值 */
		String[] ids = request.getParameterValues("ids");
		if (ids != null) {
			/* 执行删除操作，删除成功返回1，失败返回0 */
			int dels = bankcardService.deletes(ids);
			if (dels != 0) {
				responseInfo.setResult("00020");
				responseInfo.setMessage(requestContext
						.getMessage("delete.success"));
			} else {
				responseInfo.setResult("00021");
				responseInfo.setMessage(requestContext
						.getMessage("delete.fail"));
			}
		} else {
			responseInfo.setResult("00021");
			responseInfo.setMessage(requestContext.getMessage("delete.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

}
